package ${package}.${rootArtifactId}.common.util;

import ${package}.${rootArtifactId}.common.constant.NumberConstant;
import ${package}.${rootArtifactId}.common.model.BaseTableQueryParam;
import ${package}.${rootArtifactId}.common.model.PageInfoResult;
import ${package}.${rootArtifactId}.mapper.BaseDao;

import java.util.List;

/**
 * 分页工具
 *
 * @author weidong.lin
 * @date 2021/7/20 10:24
 */
public class PageHelper {
    private static final String PAGE_COUNT_METHOD = "countByPages";
    private static final String PAGE_LIST_METHOD = "getPages";

    public static <T> PageInfoResult<T> warpPage(BaseTableQueryParam param, BaseDao mapper) {
        PageInfoResult<T> pageInfo = new PageInfoResult<>(param.getPageSize(), param.getPageNum());
        SqlUtil.configOffset(param);
        Long count = ReflectUtil.invokeMethod(mapper, PAGE_COUNT_METHOD, param);
        if (NumberConstant.NUM_0L >= count) {
            return pageInfo;
        }
        List<T> list = ReflectUtil.invokeMethod(mapper, PAGE_LIST_METHOD, param);

        pageInfo.setList(list);
        pageInfo.setTotal(count);
        return pageInfo;
    }
}
